package com.kly.user.serviceImpl.traffic.wlm;

import cn.hutool.core.net.NetUtil;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.kly.user.db.entity.CustomersUserEntity;
import com.kly.user.db.entity.IssueConfigEntity;
import com.kly.user.db.entity.IssueUserPoolEntity;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.enums.AppCodeEnum;
import com.kly.user.enums.AppEnum;
import com.kly.user.enums.UserType;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
public class MebooWLM extends WLMBase {

    private String localIP = NetUtil.getLocalhostStr();

    @PostConstruct
    public void init() {
        WLMTrafficHandler.wlmBaseMap.put(AppEnum.MEBOO.getUserAppFlag(), this);
        WLMTrafficHandler.wlmBaseMap.put(AppEnum.MEBOO_IOS.getUserAppFlag(), this);
    }

    @Override
    public void dealCondition(IssueConfigEntity issueConfigEntity, UserEntity userEntity) {
        Integer wlmAmount = wlmAmount(userEntity.getUserCode());
        logger.info("==WLMIssueTask=={},userCode:{},name:{},当日已经下发:{}", localIP, userEntity.getUserCode(), userEntity.getNickName(), wlmAmount);
        if (wlmAmount >= issueConfigEntity.getIssueCount()) {
            logger.info("==WLMIssueTask 当前用户的当日wlm下发次数已经足够==,userCode:{},name:{}", userEntity.getUserCode(), userEntity.getNickName());
            return;
        }

        boolean online = userEntity.getOnline() == 1;
        //在线
        if (online) {
            int issueTime = getWLMIssueTime(issueConfigEntity, wlmAmount);
            logger.info("==WLMIssueTask 在线==,userCode:{},name:{},下次应该下发的时间mins是:{}", userEntity.getUserCode(), userEntity.getNickName(), issueTime);
            push(issueConfigEntity, wlmAmount, userEntity, issueTime);
            return;
        }

        logger.info("==WLMIssueTask 不在线==,userCode:{},name:{}", userEntity.getUserCode(), userEntity.getNickName());
        if (isToday(userEntity.getGmtModified())) {
            logger.info("==WLMIssueTask 不在线 但今天有启动过==,userCode:{},name:{}", userEntity.getUserCode(), userEntity.getNickName());
            //当天wlm数量
            //距离上次下发时间
            int IssueTime = wlmAmount * issueConfigEntity.getIssueHour();
            push(issueConfigEntity, wlmAmount, userEntity, IssueTime);
        } else {
            logger.info("==WLMIssueTask 不在线 但今天没有启动过==,userCode:{},name:{}", userEntity.getUserCode(), userEntity.getNickName());
            SimpleDateFormat format0 = new SimpleDateFormat("HHmmss");
            Time time = new Time(new Date().getTime());
            if (Integer.parseInt(format0.format(issueConfigEntity.getIssueTime())) < Integer.parseInt(format0.format(time))) {
                int IssueTime = wlmAmount * issueConfigEntity.getIssueHour();
                push(issueConfigEntity, wlmAmount, userEntity, IssueTime);
            }
        }
    }

    public void push(IssueConfigEntity issueConfigEntity, Integer wlmAmount, UserEntity userEntity, int issueTime) {
        boolean currentUserInWhiteList = checkInWhiteList(userEntity.getUserCode());
        if (wlmAmount == 0) {
            logger.info("==WLMIssueTask 今日无下发==,userCode:{},name:{},今日无下发", userEntity.getUserCode(), userEntity.getNickName());
            String userCode = getUser(userEntity.getUserCode(), userEntity.getLookingFor(),currentUserInWhiteList);
            if (userCode == null) {
                logger.info("==WLMIssueTask 今日无下发==,userCode:{},name:{},找不到目标model", userEntity.getUserCode(), userEntity.getNickName());
                return;
            }
            logger.info("==WLMIssueTask 今日无下发 找到 目标model==,userCode:{},name:{},model:{}", userEntity.getUserCode(), userEntity.getNickName(), userCode);
            addUserWlm(userEntity.getUserCode(), userCode, userEntity.getAppCode());
            return;
        }

        logger.info("==WLMIssueTask 今日有下发==,userCode:{},name:{},今日无下发", userEntity.getUserCode(), userEntity.getNickName());
        if (wlmAmount < issueConfigEntity.getIssueCount()) {
            long distanceWLMTime = distanceWLMTime(userEntity.getUserCode());
            if (distanceWLMTime > issueTime) {
                //下发
                String userCode = getUser(userEntity.getUserCode(), userEntity.getLookingFor(),currentUserInWhiteList);
                if (userCode != null) {
                    addUserWlm(userEntity.getUserCode(), userCode, userEntity.getAppCode());
                }
            }
        }
    }



}
